package org.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.reggie.common.R;
import org.example.reggie.pojo.Exam;
import org.example.reggie.pojo.List;
import org.example.reggie.service.ExamService;
import org.example.reggie.service.ListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/exam")
public class ExamController {
    @Autowired
    private ExamService examService;
    @Autowired
    private ListService listService;

    @PostMapping()
    public R<String> addExam(HttpServletRequest request , @RequestBody Exam exam){

        Long empId = (Long) request.getSession().getAttribute("employee");
        exam.setCreater(empId);

        examService.save(exam);

        return R.success("考试添加成功");
    }

    @PostMapping("registerExam")
    public R<String> registerExam(HttpServletRequest request , @RequestBody Exam exam){
        Long SId = (Long) request.getSession().getAttribute("student");
        LambdaQueryWrapper<List> queryWrapper1 = new LambdaQueryWrapper<List>();
        queryWrapper1.eq(List::getId,exam.getId()).eq(List::getSid,SId);
        List same = listService.getOne(queryWrapper1);
        if(same == null){
            List list1 = new List();
            list1.setExamName(exam.getExamname());
            list1.setExamId(exam.getId());
            list1.setPlace(exam.getPlace());
            list1.setCreater(exam.getCreater());
            list1.setScore("");
            list1.setSid(SId);

            int i = 1;
            while(i < Integer.valueOf(exam.getNumber()).intValue()){
                LambdaQueryWrapper<List> queryWrapper = new LambdaQueryWrapper<List>();
                queryWrapper.eq(List::getSite,String.valueOf(i + 1)).eq(List::getExamId,exam.getId());
                List check  = listService.getOne(queryWrapper);
                if(check == null){
                    break;
                }else {
                    i++;
                }
            }
            if(i == Integer.valueOf(exam.getNumber()).intValue()){
                return R.error("该考试人数已满");
            }else {
                list1.setSite(String.valueOf(i+1));
                listService.save(list1);
                return R.success("考试报名成功");
            }
        }else {
            return R.error("已经报名成功请勿重复报名");
        }

    }

    @PutMapping()
    public R<String> update(HttpServletRequest request,@RequestBody Exam exam){

        examService.updateById(exam);

        return R.success("修改成功");
    }
    @GetMapping("/{id}")
    public R<Exam> getExamById(@PathVariable Long id){

        Exam exam = examService.getById(id);

        return R.success(exam);
    }
    @PutMapping("score")
    public R<String> addScore(HttpServletRequest request,@RequestBody List list){
        String score = list.getScore();
        if(score != null) {
            list.setScore1(Integer.parseInt(score));
            list.setStatus("2");
        }
        listService.updateById(list);

        return R.success("修改成功");
    }

    @GetMapping("score/{id}")
    public R<List> getById(@PathVariable Long id){
        List list = listService.getById(id);

        return R.success(list);
    }
    @GetMapping("page")
    public R<Page> page(int page,int pageSize,String name){


        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Exam> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(name != null,Exam::getExamname,name);

        examService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @GetMapping("studentPage")
    public R<Page> studentPage(int page,int pageSize,String name){

        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Exam> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(name != null,Exam::getExamname,name);
        queryWrapper.eq(Exam::getStatus,"0");
        
        examService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }


    @GetMapping("scorePage")
    public R<Page> scorePage(int page,int pageSize,String name, HttpServletRequest request){
        Page pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<List> queryWrapper = new LambdaQueryWrapper<List>();
        Long SId = (Long) request.getSession().getAttribute("student");
        queryWrapper.eq(List::getSid,SId);
        queryWrapper.like(name != null,List::getExamName,name);

//        queryWrapper.eq();

        listService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }


    @GetMapping("studentScorePage")
    public R<Page> studentScorePage(int page,int pageSize,String name,HttpServletRequest request){
        Page pageInfo = new Page(page,pageSize);
        LambdaQueryWrapper<List> queryWrapper = new LambdaQueryWrapper<List>();
        Long creater = (Long) request.getSession().getAttribute("employee");
        System.out.println("creater:"+creater);
//        String creater1 = (String) request.getSession().getAttribute("employee");
//        if(creater1 != "1846748277652996097") {
            queryWrapper.eq(List::getCreater, creater);
            if (name != null) {
                queryWrapper.like(name != null, List::getExamName, name);
                queryWrapper.orderByDesc(List::getScore1);
            }
//        }
        listService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

}
